Here are some sample plots using the voting data. Any thoughts about what this tells us for analysis?
Setup
Note the setup variables below. These are supposed to be controls all the plots. Sometimes they are. Sometimes not. Have to clean this up, but the goal is uniformity across all the plots.
For fonts, I tried to use ‘sans’ which should pick the system sans serif font for whatever OS is running. In the plotly plot I had to pick a specific font or it defaults to Times New Roman. I picked Arial, but we should look at including Helvetica for macOS users.
The following objects are masked from 'package:stats':
filter, lag
The following objects are masked from 'package:base':
intersect, setdiff, setequal, union
library(knitr)library(readr)library(sf)
Linking to GEOS 3.13.0, GDAL 3.8.5, PROJ 9.5.1; sf_use_s2() is TRUE
library(tigris)
To enable caching of data, set `options(tigris_use_cache = TRUE)`
in your R script or .Rprofile.
library(plotly)
Attaching package: 'plotly'
The following object is masked from 'package:ggplot2':
last_plot
The following object is masked from 'package:stats':
filter
The following object is masked from 'package:graphics':
layout
# --- Global color palette (Civic Triangle style) ---fill_col <-"#ffffff"# white backgroundline_col <-"#3a5f7d"# blue-grey for roads and outlinestext_col <-"#2f3b44"# text and titlesalt_line <-"#536cae"# secondary line coloralt_text <-"#536cae"# secondary text colorborder_col <-"#e6eef5"# light blue-grey for county borders
Figure 7: Mean Years of Schooling by County in Texas (2020)
# --- Export static version if needed ---ggsave("mean_years_choropleth.png", plot = p_mean_years,width =9, height =8, dpi =300, units ="in", limitsize =FALSE)
Figure 7
library(ggplot2)library(dplyr)# --- Prepare data ---# Assume `turnout` already contains Turnout_Rate and edu_index# Sort counties by turnout descendingbar_data <- turnout %>%arrange(desc(Turnout_Rate)) %>%mutate(County =factor(County, levels =rev(County))) # top = highest turnout# --- Create the “double bar” style plot ---p_double <-ggplot(bar_data) +# Left side: voter participationgeom_bar(aes(x =-Turnout_Rate, y = County),stat ="identity", fill = line_col, alpha =0.8, width =0.8) +# Right side: education indexgeom_bar(aes(x = edu_index *100, y = County),stat ="identity", fill = alt_line, alpha =0.8, width =0.8) +# Center linegeom_vline(xintercept =0, color = border_col, linewidth =0.8) +# Axis and labelsscale_x_continuous(name =NULL,limits =c(-max(bar_data$Turnout_Rate, na.rm =TRUE),max(bar_data$edu_index, na.rm =TRUE) *100),breaks =seq(-100, 100, by =25),labels =function(x) abs(x) ) +labs(y =NULL,title ="Voting Participation vs. Education Index by County (Texas, 2020)",subtitle ="Counties sorted by Voter Participation (Highest → Lowest)" ) +theme_minimal(base_family ="sans") +theme(plot.background =element_rect(fill = fill_col, color =NA),panel.grid.major.y =element_blank(),panel.grid.minor =element_blank(),axis.text.y =element_blank(),axis.ticks.y =element_blank(),axis.text.x =element_text(color = text_col, size =10),axis.title.x =element_text(color = text_col, face ="bold"),plot.title =element_text(color = text_col, face ="bold", size =16, hjust =0.5),plot.subtitle =element_text(color = text_col, size =12, hjust =0.5) ) +annotate("text", x =-90, y =nrow(bar_data) +2,label ="Voter Turnout (%)", color = line_col,family ="sans", fontface ="bold", size =4.2) +annotate("text", x =90, y =nrow(bar_data) +2,label ="Education Index (0–1 → %)", color = alt_text,family ="sans", fontface ="bold", size =4.2)# --- Display inline ---p_double
Warning: Removed 1 row containing missing values or values outside the scale range
(`geom_text()`).
Removed 1 row containing missing values or values outside the scale range
(`geom_text()`).
# --- Export PNG for publication use ---ggsave("double_bar_voting_education.png", plot = p_double,width =10, height =10, dpi =300, units ="in", limitsize =FALSE)
Warning: Removed 1 row containing missing values or values outside the scale range
(`geom_text()`).
Removed 1 row containing missing values or values outside the scale range
(`geom_text()`).
Figure 8: County-level comparison of Voter Participation and Education Index (2020)
Figure 8
library(ggplot2)library(dplyr)# --- Prepare data ---# Sort counties by education index descendingbar_data_edu <- turnout %>%arrange(desc(edu_index)) %>%mutate(County =factor(County, levels =rev(County))) # top = highest education# --- Create the mirrored “violin-style” double bar chart ---p_double_edu <-ggplot(bar_data_edu) +# Left side: voter participationgeom_bar(aes(x =-Turnout_Rate, y = County),stat ="identity", fill = line_col, alpha =0.8, width =0.8) +# Right side: education indexgeom_bar(aes(x = edu_index *100, y = County),stat ="identity", fill = alt_line, alpha =0.8, width =0.8) +# Center linegeom_vline(xintercept =0, color = border_col, linewidth =0.8) +# Axes and scalesscale_x_continuous(name =NULL,limits =c(-max(bar_data_edu$Turnout_Rate, na.rm =TRUE),max(bar_data_edu$edu_index, na.rm =TRUE) *100),breaks =seq(-100, 100, by =25),labels =function(x) abs(x) ) +labs(y =NULL,title ="Education Index vs. Voter Participation by County (Texas, 2020)",subtitle ="Counties sorted by Education Index (Highest → Lowest)" ) +theme_minimal(base_family ="sans") +theme(plot.background =element_rect(fill = fill_col, color =NA),panel.grid.major.y =element_blank(),panel.grid.minor =element_blank(),axis.text.y =element_blank(),axis.ticks.y =element_blank(),axis.text.x =element_text(color = text_col, size =10),axis.title.x =element_text(color = text_col, face ="bold"),plot.title =element_text(color = text_col, face ="bold", size =16, hjust =0.5),plot.subtitle =element_text(color = text_col, size =12, hjust =0.5) ) +annotate("text", x =-90, y =nrow(bar_data_edu) +2,label ="Voter Turnout (%)", color = line_col,family ="sans", fontface ="bold", size =4.2) +annotate("text", x =90, y =nrow(bar_data_edu) +2,label ="Education Index (0–1 → %)", color = alt_text,family ="sans", fontface ="bold", size =4.2)# --- Display inline ---p_double_edu
Warning: Removed 1 row containing missing values or values outside the scale range
(`geom_text()`).
Removed 1 row containing missing values or values outside the scale range
(`geom_text()`).
# --- Export PNG for publication use ---ggsave("double_bar_education_voting.png", plot = p_double_edu,width =10, height =10, dpi =300, units ="in", limitsize =FALSE)
Warning: Removed 1 row containing missing values or values outside the scale range
(`geom_text()`).
Removed 1 row containing missing values or values outside the scale range
(`geom_text()`).
Figure 9: County-level comparison of Education Index and Voter Participation (2020)